package com.punai.customer.controller;

import com.punai.common.annotation.Log;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.enums.BusinessType;
import com.punai.common.utils.StringUtils;
import com.punai.customer.domain.CusGxLinkGk;
import com.punai.customer.domain.contants.CusConstants;
import com.punai.customer.domain.vo.CusGxFeatureLinkGkVo;
import com.punai.customer.domain.vo.CusLinkGkVo;
import com.punai.customer.service.ICusGxLinkGkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 标准费用项关联归口Controller
 * 
 * @author zhx
 * @date 2023-03-28
 */
@RestController
@RequestMapping("/cus/cusGxLinkGk")
public class CusGxLinkGkController extends BaseController
{
    @Autowired
    private ICusGxLinkGkService cusGxLinkGkService;

    /**
     * 查询标准费用项关联归口列表
     */
    @PreAuthorize("@ss.hasPermi('cus:cusGxLinkGk:list')")
    @GetMapping("/list")
    public TableDataInfo list(CusGxLinkGk cusGxLinkGk)
    {
        cusGxLinkGk.setCompanyId(getCompanyId());
        return cusGxLinkGkService.selectCusGxLinkGkList(cusGxLinkGk);
    }

    @PreAuthorize("@ss.hasPermi('cus:cusGxLinkGk:list')")
    @GetMapping("/getChildren/{gxId}")
    public TableDataInfo getChildren(@PathVariable("gxId") String gxId)
    {
        return getDataTable(cusGxLinkGkService.getChildren(gxId,getCompanyId()));
    }

    /**
     * 获取标准费用项关联归口详细信息
     */
    @PreAuthorize("@ss.hasPermi('cus:cusGxLinkGk:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(cusGxLinkGkService.selectCusGxLinkGkById(id));
    }

    /**
     * 新增标准费用项关联归口
     */
    @PreAuthorize("@ss.hasPermi('cus:cusGxLinkGk:add')")
    @Log(title = CusConstants.GX_NAME + "关联归口", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CusGxLinkGk cusGxLinkGk)
    {
        return toAjax(cusGxLinkGkService.insertCusGxLinkGk(cusGxLinkGk));
    }

    /**
     * 新增标准费用项关联归口特征
     */
//    @PreAuthorize("@ss.hasPermi('cus:cusGxLinkGk:add')")
    @Log(title = CusConstants.GX_NAME + "根据特征关联归口", businessType = BusinessType.INSERT)
    @PostMapping("/addFeatureLinkGk")
    public AjaxResult addFeatureLinkGk(@RequestBody List<CusGxFeatureLinkGkVo> cusGxFeatureLinkGkVos)
    {
        return AjaxResult.success(cusGxLinkGkService.insertCusGxFeatureLinkGk(cusGxFeatureLinkGkVos,getCompanyId()));
    }

    /**
     * 修改标准费用项关联归口
     */
//    @PreAuthorize("@ss.hasPermi('cus:cusGxLinkGk:edit')")
    @Log(title = CusConstants.GX_NAME + "关联归口", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CusLinkGkVo cusLinkGkVo)
    {
        return toAjax(cusGxLinkGkService.updateCusGxLinkGk(cusLinkGkVo));
    }

//    @PreAuthorize("@ss.hasPermi('cus:cusGxLinkGk:edit')")
    @Log(title = CusConstants.GX_NAME + "分包模式", businessType = BusinessType.UPDATE)
    @PutMapping("/setSubpackagePattern")
    public AjaxResult setSubpackagePattern(@RequestBody CusLinkGkVo cusLinkGkVo)
    {
        return toAjax(cusGxLinkGkService.setSubpackagePattern(getCompanyId(),cusLinkGkVo));
    }

    /**
     * 删除标准费用项关联归口
     */
    @PreAuthorize("@ss.hasPermi('cus:cusGxLinkGk:remove')")
    @Log(title = CusConstants.GX_NAME + "关联归口", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(cusGxLinkGkService.deleteCusGxLinkGkByIds(ids));
    }

    /**
     * 获取标准费用项关联特征
     */
    @GetMapping(value = "/getGxLinkFeature/{id}")
    public AjaxResult getGxLinkFeature(@PathVariable("id") String id)
    {
        return AjaxResult.success(cusGxLinkGkService.getGxLinkFeature(id,getCompanyId()));
    }

    @Log(title = CusConstants.GX_NAME + "清空关联归口", businessType = BusinessType.DELETE)
    @DeleteMapping("/clearLinkGk/{id}/{specId}")
    public AjaxResult remove(@PathVariable String id,@PathVariable String specId)
    {
        return toAjax(cusGxLinkGkService.clearLinkGk(id,specId,getCompanyId()));
    }

    @Log(title = CusConstants.GX_NAME + "特征关联归口", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteFeatureLinkGk/{id}/{gxId}")
    public AjaxResult deleteFeatureLinkGk(@PathVariable("id") String id,@PathVariable("gxId") String gxId)
    {
        return toAjax(cusGxLinkGkService.deleteFeatureLinkGk(id,gxId,getCompanyId()));
    }

    @GetMapping("/getHistoryGx/{specId}/{gkType}")
    public TableDataInfo getHistoryGx(@PathVariable("specId") String specId,@PathVariable("gkType") String gkType)
    {
        startPage();
        if(StringUtils.isEmpty(specId)){
            throw new RuntimeException("专业获取失败");
        }
        if(StringUtils.isEmpty(gkType)){
            throw new RuntimeException("归口类型获取失败");
        }
        return getDataTable(cusGxLinkGkService.getHistoryGx(specId,gkType));
    }
}
